package com.cws.project.cws.signin.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.cws.common.utils.poi.ExcelUtil;
import com.cws.framework.aspectj.lang.annotation.Log;
import com.cws.framework.aspectj.lang.enums.BusinessType;
import com.cws.framework.web.controller.BaseController;
import com.cws.framework.web.domain.AjaxResult;
import com.cws.framework.web.page.TableDataInfo;
import com.cws.project.cws.clazz.domain.ClazzUser;
import com.cws.project.cws.clazz.service.IClazzUserService;
import com.cws.project.cws.course.domain.Course;
import com.cws.project.cws.course.service.ICourseService;
import com.cws.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cws.project.cws.signin.domain.SignIn;
import com.cws.project.cws.signin.service.ISignInService;

/**
 * 签到管理Controller
 *
 * @author xyl
 * @date 2022-03-27
 */
@Controller
@RequestMapping("/cws/signin")
public class SignInController extends BaseController {
    private String prefix = "cws/signin";

    @Autowired
    private ISignInService signInService;
    @Autowired
    private ICourseService courseService;
    @Autowired
    private IClazzUserService clazzUserService;

    @RequiresPermissions("cws:signin:view")
    @GetMapping()
    public String signin() {
        return prefix + "/signin";
    }

    /**
     * 查询签到管理列表
     */
    @RequiresPermissions("cws:signin:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SignIn signIn) {
        //todo 学生无权限

        User sysUser = getSysUser();
        //辅导员 查看自己班的
        if(sysUser.isInstructor()){
            ClazzUser clazzUser = new ClazzUser();
            clazzUser.setUserId(sysUser.getUserId());
            signIn.setClazzIds(clazzUserService.selectClazzUserList(clazzUser).stream().map(ClazzUser::getClazzId).collect(Collectors.toList()));
        }
        //教师
        if(sysUser.isTeacher() || sysUser.isStudent()){
            signIn.setTeacherId(sysUser.getUserId());
        }
        startPage();
        List<SignIn> list = signInService.selectSignInList(signIn);
        return getDataTable(list);
    }

    /**
     * 导出签到管理列表
     */
    @RequiresPermissions("cws:signin:export")
    @Log(title = "签到管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SignIn signIn) {
        List<SignIn> list = signInService.selectSignInList(signIn);
        ExcelUtil<SignIn> util = new ExcelUtil<SignIn>(SignIn.class);
        return util.exportExcel(list, "signin");
    }

    /**
     * 新增签到管理
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        User sysUser = getSysUser();
        List<Course> courseList;
        Course course = new Course();
        if(sysUser.isTeacher()){
            course.setTeacherId(sysUser.getUserId());
        }
        if(sysUser.isInstructor()){
            ClazzUser clazzUser = new ClazzUser();
            clazzUser.setUserId(sysUser.getUserId());
            course.setClazzIds(clazzUserService.selectClazzUserList(clazzUser).stream().map(ClazzUser::getClazzId).collect(Collectors.toList()));
        }
        courseList = courseService.selectCourseList(course);
        mmap.put("courses",courseList);
        return prefix + "/add";
    }

    /**
     * 新增保存签到管理
     */
    @RequiresPermissions("cws:signin:add")
    @Log(title = "签到管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SignIn signIn) {
        return toAjax(signInService.insertSignIn(signIn));
    }

    /**
     * 修改签到管理
     */
    @GetMapping("/edit/{signInId}")
    public String edit(@PathVariable("signInId") Long signInId, ModelMap mmap) {
        SignIn signIn = signInService.selectSignInById(signInId);
        mmap.put("signIn", signIn);
        User sysUser = getSysUser();
        List<Course> courseList;
        if(sysUser.isAdmin()){
            courseList = courseService.selectCourseList(null);
        }else{
            Course course = new Course();
            course.setTeacherId(sysUser.getUserId());
            courseList = courseService.selectCourseList(course);
        }
        mmap.put("courses",courseList);
        return prefix + "/edit";
    }

    /**
     * 修改保存签到管理
     */
    @RequiresPermissions("cws:signin:edit")
    @Log(title = "签到管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SignIn signIn) {
        return toAjax(signInService.updateSignIn(signIn));
    }

    /**
     * 删除签到管理
     */
    @RequiresPermissions("cws:signin:remove")
    @Log(title = "签到管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(signInService.deleteSignInByIds(ids));
    }
}
